package chapter07

object Demo13_SimpleFunction {
  def main(args: Array[String]): Unit = {

    val list1 = List(2, 3, 4, 5, 6, 7, 8, 7, 66, 55, 44)
    val list2 = List(("a", 1), ("b", 2), ("c", 3), ("d", 4), ("e", 5))
    //      (1)求和
    var sum = 0
    for (elem <- list1) {
      sum += elem
    }
    println(sum)

    println(list1.sum)

    //      (2)求乘积
    println(list1.product)
    //      (3）最大
    println(list1.max)
    println("最大值"+list2.max)
    println(list2.maxBy((tuple: (String, Int)) => tuple._2)) //tuple._x 表示取出来在元组某个位置的值 tuple._2取第二个值
    //  (4）最小值
    println(list1.min)
    println(list2.minBy(_._2))
    //      (5）排序
    //sorted
    //从小到大进行排序
    val sorted = list1.sorted
    println(sorted)

    //从大到小逆序排序
    println(sorted.sorted.reverse)
    //传入隐式参数
    println(list1.sorted(Ordering[Int].reverse))

    //sortBy
    println(list2.sortBy(_._2))
    println(list2.sortBy(_._2)(Ordering[Int].reverse))

    //sortWith
    println(list1.sortWith((a: Int , b: Int) => {a < b}))
    //简化
    println(list1.sortWith(_<_))
    println(list1.sortWith(_>_))
  }
}
